
// 复制node数据
export const getNodeModel = (combo: any) => {
    // 复制当前node
    const nodes = combo.getNodes().map((node: any) => {
        let n = node.getModel()
        delete n.comboId
        delete n.id
        //  确定node相对于ombo的位置
        n.position = {
            x: combo.getModel().x - n.x,
            y: combo.getModel().y - n.y,
        }
        return n
    })

    return nodes || []
}
// 获取combo
/**
  * @getNodes  获取combo下的node
  * @getCombos 获取combo下的子combo
  */
export const getComboModel = (combo: any, result: any = []) => {
    // 获取子nodes[]
    const no = getNodeModel(combo)
    // 获取子combos[]
    const cs = combo.getCombos()

}

type ShapePosition = [number, number][]
// 获取锚点做标的函数
export const getShapePosition = (type: string): ShapePosition => {
    let anchroPosition: ShapePosition = [[0.5, 0], [1, 0.5], [0.5, 1], [0, 0.5]] // 圆形和方形锚点坐标
    let trianglePosition: ShapePosition = [[0.5, 0], [1, 1], [0, 1]] // 三角形锚点坐标
    let starPosition: ShapePosition = [[0.5, 0], [1, 0.3825], [0.81, 1], [0.19, 1], [0, 0.3825]] // 五角星锚点坐标
    switch (type) {
        case 'rect-node':
            return anchroPosition
        case 'circle-node':
            return anchroPosition
        case 'star-node':
            return starPosition
        case 'triangle-node':
            return trianglePosition
        default:
            return anchroPosition
    }
}

type UpdateShapePosition = {
    x: number,
    y: number
}
// 更新锚点位置的函数
export const updateShapePosition = (model: any, box: any, i: number): UpdateShapePosition => {
    let position:UpdateShapePosition = {
        x:0,
        y:0
    }
    if (model.type === "circle-node" || model.type === "rect-node") {
        position = {
            x: (typeFlag(model.size)[0] * getShapePosition(model.type)[i][0]) - (typeFlag(model.size)[0] / 2),
            y: (typeFlag(model.size)[1] * getShapePosition(model.type)[i][1]) - (typeFlag(model.size)[1] / 2)
        }
    }
    if (model.type==='triangle-node') {
        position = {
            x: box.x + box.width * getShapePosition(model.type)[i][0],
            y: box.x + box.width * getShapePosition(model.type)[i][1]
        }
    }

    if (model.type === 'star-node') {
        position = {
            x: box.width * getShapePosition(model.type)[i][0] - box.width / 2,
            y: box.height * getShapePosition(model.type)[i][1]- box.height / 2 - box.height * 0.05
        }
    }
    return position
}
// 转换size结构
const typeFlag = (size: number | number[]) => {
    if (typeof size === 'number') {
        return [size, size]
    }
    return size
}